package com.inei.eea2014.model.dao;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.inei.eea2014.model.connection.UtilDB;
import com.inei.eea2014.model.entity.Capitulo2;
import com.inei.eea2014.model.entity.Capitulo3;

public class Capitulo3Dao implements ICRUD<Capitulo3> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public Capitulo3Dao(Context context) {
		util = new UtilDB(context);
	}

	// metodos del CRUD
	@Override
	public Capitulo3 buscar(Capitulo3 o) {
		Capitulo3 obj = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery(
				"select * from Capitulo3 where RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			obj = new Capitulo3();
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setUSUARIO(cursor.getString(cursor.getColumnIndex("USUARIO")));
			obj.setC0003_001_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_001_01_N")));
			obj.setC0003_001_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_001_02_N")));
			obj.setC0003_001_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_001_03_N")));
			obj.setC0003_001_04_N(cursor.getInt(cursor
					.getColumnIndex("C0003_001_04_N")));
			obj.setC0003_002_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_002_01_N")));
			obj.setC0003_002_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_002_02_N")));
			obj.setC0003_002_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_002_03_N")));
			obj.setC0003_002_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_002_04_N")));
			obj.setC0003_003_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_003_01_N")));
			obj.setC0003_003_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_003_02_N")));
			obj.setC0003_003_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_003_03_N")));
			obj.setC0003_003_04_N(cursor.getInt(cursor
					.getColumnIndex("C0003_003_04_N")));
			obj.setC0003_004_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_004_01_N")));
			obj.setC0003_004_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_004_02_N")));
			obj.setC0003_004_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_004_03_N")));
			obj.setC0003_004_04_N(cursor.getInt(cursor
					.getColumnIndex("C0003_004_04_N")));
			obj.setC0003_005_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_005_01_N")));
			obj.setC0003_005_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_005_02_N")));
			obj.setC0003_005_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_005_03_N")));
			obj.setC0003_005_04_N(cursor.getInt(cursor
					.getColumnIndex("C0003_005_04_N")));
			obj.setC0003_006_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_006_01_N")));
			obj.setC0003_006_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_006_02_N")));
			obj.setC0003_006_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_006_03_N")));
			obj.setC0003_006_04_N(cursor.getInt(cursor
					.getColumnIndex("C0003_006_04_N")));
			obj.setC0003_007_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_007_01_N")));
			obj.setC0003_007_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_007_02_N")));
			obj.setC0003_007_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_007_03_N")));
			obj.setC0003_007_04_N(cursor.getInt(cursor
					.getColumnIndex("C0003_007_04_N")));
			obj.setC0003_008_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_008_01_N")));
			obj.setC0003_008_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_008_02_N")));
			obj.setC0003_008_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_008_03_N")));
			obj.setC0003_008_04_N(cursor.getInt(cursor
					.getColumnIndex("C0003_008_04_N")));
			obj.setC0003_009_01_N(cursor.getInt(cursor
					.getColumnIndex("C0003_009_01_N")));
			obj.setC0003_009_02_N(cursor.getInt(cursor
					.getColumnIndex("C0003_009_02_N")));
			obj.setC0003_009_03_N(cursor.getInt(cursor
					.getColumnIndex("C0003_009_03_N")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Capitulo3> leer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int crear(Capitulo3 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("RUC", o.getRUC());
		valores.put("USUARIO", o.getUSUARIO());
		valores.put("C0003_001_01_N", o.getC0003_001_01_N());
		valores.put("C0003_001_02_N", o.getC0003_001_02_N());
		valores.put("C0003_001_03_N", o.getC0003_001_03_N());
		valores.put("C0003_001_04_N", o.getC0003_001_04_N());
		valores.put("C0003_002_01_N", o.getC0003_002_01_N());
		valores.put("C0003_002_02_N", o.getC0003_002_02_N());
		valores.put("C0003_002_03_N", o.getC0003_002_03_N());
		valores.put("C0003_002_04_N", o.getC0003_002_04_N());
		valores.put("C0003_003_01_N", o.getC0003_003_01_N());
		valores.put("C0003_003_02_N", o.getC0003_003_02_N());
		valores.put("C0003_003_03_N", o.getC0003_003_03_N());
		valores.put("C0003_003_04_N", o.getC0003_003_04_N());
		valores.put("C0003_004_01_N", o.getC0003_004_01_N());
		valores.put("C0003_004_02_N", o.getC0003_004_02_N());
		valores.put("C0003_004_03_N", o.getC0003_004_03_N());
		valores.put("C0003_004_04_N", o.getC0003_004_04_N());
		valores.put("C0003_005_01_N", o.getC0003_005_01_N());
		valores.put("C0003_005_02_N", o.getC0003_005_02_N());
		valores.put("C0003_005_03_N", o.getC0003_005_03_N());
		valores.put("C0003_005_04_N", o.getC0003_005_04_N());
		valores.put("C0003_006_01_N", o.getC0003_006_01_N());
		valores.put("C0003_006_02_N", o.getC0003_006_02_N());
		valores.put("C0003_006_03_N", o.getC0003_006_03_N());
		valores.put("C0003_006_04_N", o.getC0003_006_04_N());
		valores.put("C0003_007_01_N", o.getC0003_007_01_N());
		valores.put("C0003_007_02_N", o.getC0003_007_02_N());
		valores.put("C0003_007_03_N", o.getC0003_007_03_N());
		valores.put("C0003_007_04_N", o.getC0003_007_04_N());
		valores.put("C0003_008_01_N", o.getC0003_008_01_N());
		valores.put("C0003_008_02_N", o.getC0003_008_02_N());
		valores.put("C0003_008_03_N", o.getC0003_008_03_N());
		valores.put("C0003_008_04_N", o.getC0003_008_04_N());
		valores.put("C0003_009_01_N", o.getC0003_009_01_N());
		valores.put("C0003_009_02_N", o.getC0003_009_02_N());
		valores.put("C0003_009_03_N", o.getC0003_009_03_N());

		resultado = (int) db.insert("Capitulo3", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int actualizar(Capitulo3 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("C0003_001_01_N", o.getC0003_001_01_N());
		valores.put("C0003_001_02_N", o.getC0003_001_02_N());
		valores.put("C0003_001_03_N", o.getC0003_001_03_N());
		valores.put("C0003_001_04_N", o.getC0003_001_04_N());
		valores.put("C0003_002_01_N", o.getC0003_002_01_N());
		valores.put("C0003_002_02_N", o.getC0003_002_02_N());
		valores.put("C0003_002_03_N", o.getC0003_002_03_N());
		valores.put("C0003_002_04_N", o.getC0003_002_04_N());
		valores.put("C0003_003_01_N", o.getC0003_003_01_N());
		valores.put("C0003_003_02_N", o.getC0003_003_02_N());
		valores.put("C0003_003_03_N", o.getC0003_003_03_N());
		valores.put("C0003_003_04_N", o.getC0003_003_04_N());
		valores.put("C0003_004_01_N", o.getC0003_004_01_N());
		valores.put("C0003_004_02_N", o.getC0003_004_02_N());
		valores.put("C0003_004_03_N", o.getC0003_004_03_N());
		valores.put("C0003_004_04_N", o.getC0003_004_04_N());
		valores.put("C0003_005_01_N", o.getC0003_005_01_N());
		valores.put("C0003_005_02_N", o.getC0003_005_02_N());
		valores.put("C0003_005_03_N", o.getC0003_005_03_N());
		valores.put("C0003_005_04_N", o.getC0003_005_04_N());
		valores.put("C0003_006_01_N", o.getC0003_006_01_N());
		valores.put("C0003_006_02_N", o.getC0003_006_02_N());
		valores.put("C0003_006_03_N", o.getC0003_006_03_N());
		valores.put("C0003_006_04_N", o.getC0003_006_04_N());
		valores.put("C0003_007_01_N", o.getC0003_007_01_N());
		valores.put("C0003_007_02_N", o.getC0003_007_02_N());
		valores.put("C0003_007_03_N", o.getC0003_007_03_N());
		valores.put("C0003_007_04_N", o.getC0003_007_04_N());
		valores.put("C0003_008_01_N", o.getC0003_008_01_N());
		valores.put("C0003_008_02_N", o.getC0003_008_02_N());
		valores.put("C0003_008_03_N", o.getC0003_008_03_N());
		valores.put("C0003_008_04_N", o.getC0003_008_04_N());
		valores.put("C0003_009_01_N", o.getC0003_009_01_N());
		valores.put("C0003_009_02_N", o.getC0003_009_02_N());
		valores.put("C0003_009_03_N", o.getC0003_009_03_N());

		resultado = (int) db.update("Capitulo3", valores,
				"RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Capitulo3 o) {
		// TODO Auto-generated method stub
		return 0;
	}

	// otros metodos
	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
